package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String PREFIX = "nursingPlan:all";

    /**
     * 根据id查询护理计划信息
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        /*先查询出护理计划的基本信息*/
        NursingPlan nursingPlan = this.getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        /*再把通过项目id查询出的该计划关联的所有项目，set给该计划*/
        List<NursingProjectPlanVo> projectPlanVos = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 分页条件查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        redisCache.deleteObject(PREFIX);
        /*先添加护理计划的单表基本信息,并且需要设置主键返回*/
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(DateTime.now());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        /*再批量添加和护理计划有关联的护理项目信息*/
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        return nursingPlanMapper.batchInsert(projectPlans);
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        redisCache.deleteObject(PREFIX);
        /*把dto→pojo*/
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        Long planId = nursingPlan.getId();
        nursingPlan.setUpdateTime(DateTime.now());
        /*查询出该计划下面的所有项目*/
        List<NursingProjectPlanVo> projectPlanVos = nursingProjectPlanMapper.selectByPlanId(planId);
        if(projectPlanVos != null && !projectPlanVos.isEmpty()) {

            /*先删除所有的护理项目*/
            nursingProjectPlanMapper.deleteProjectPlanByPlanId(planId);
            /*再批量添加护理项目,同时把添加的护理项目绑定到该护理计划中*/
            List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
            if(projectPlans != null && !projectPlans.isEmpty()){
                projectPlans.forEach(projectPlan -> projectPlan.setPlanId(planId));
                nursingPlanMapper.batchInsert(projectPlans);
            }
        }
        /*修改护理计划的基本信息*/
        return nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        redisCache.deleteObject(PREFIX);
        for (Long id : ids) {
            nursingProjectPlanMapper.deleteProjectPlanByPlanId(id);
        }
        return this.removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        redisCache.deleteObject(PREFIX);
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> listAll() {
        List<NursingPlan> cacheList = redisCache.getCacheList(PREFIX);
        if(CollUtil.isEmpty(cacheList)){
            LambdaQueryWrapper< NursingPlan>  queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingPlan::getStatus, 1);
            List<NursingPlan> list = this.list(queryWrapper);
            redisCache.setCacheList(PREFIX, list);
            return list;
        }
        return cacheList;
    }
}
